// resource/components/list.vue
<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form ref="form" :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="form.name" clearable placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="form.url" clearable placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="categoryId">
            <el-select v-model="form.categoryId" clearable placeholder="资源分类">
              <el-option v-for="item in resourcesCategory" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
              <el-button @click="onReset">
                 重置
              </el-button>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="isLoading" :data="resources" style="width: 100%">
        <el-table-column
          type="index"
          label="编号"
          width="100"
        ></el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="资源名称"
          width="100"
        ></el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="添加日期">
            <template slot-scope="scope">
                <span>{{ scope.row.createdTime | dataFormat }}</span>
            </template>
        </el-table-column>
        <el-table-column prop="url" label="资源路径"> </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button size="mini" @click='handleEdit(scope.row)'>编辑</el-button>
                <el-button size="mini" @click='handleDel(scope.row)' type="danger">删除</el-button>
            </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="form.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount">
    </el-pagination>
  </div>
</template>

<script>
import { getResourcePages, getResourceCategories } from '@/services/resource'
function isaddZero (data) {
  return data >= 10 ? data : ('0' + data)
}
export default {
  name: 'ResourceList',
  data () {
    return {
      resources: [],
      resourcesCategory: [],
      form: {
        current: 0,
        size: 10,
        name: '',
        categoryId: ''
      },
      totalCount: 0,
      isLoading: true
    }
  },
  created () {
    this.loadResources()
    this.loadResourceCategories()
  },
  methods: {
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      if (data.code === '000000') {
        this.resourcesCategory = data.data
      }
    },
    async loadResources () {
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.totalCount = data.data.total
        this.isLoading = false
      }
    },
    handleEdit (row) {

    },
    handleDel (row) {
    },
    handleSizeChange (val) {
      this.form.size = val
      this.current = 0
      this.loadResources()
    },
    handleCurrentChange (val) {
      this.form.current = val
      this.loadResources()
    },
    onSubmit () {
      this.form.current = 0
      this.loadResources()
    },
    onReset () {
      this.$refs.form.resetFields()
    }
  },
  filters: {
    dataFormat (data) {
      data = new Date(data)
      return `${data.getFullYear()}-${isaddZero(data.getMonth() + 1)}-${isaddZero(data.getDate())}
                ${isaddZero(data.getHours())}:${isaddZero(data.getMinutes())}:${isaddZero(data.getSeconds())}
          `
    }
  }
}
</script>

<style lang="scss" scoped></style>
